package Reflection_frame;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class Demo
{
	@Test
	public void demo1()
	{
		//将调狗的吃饭方法改成调用猫的吃饭方法,不用反射
//		Dog dog = new Dog();
//		dog.eat();
		Cat cat = new Cat();
		cat.eat();
	}

	@Test
	public void demo2() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
	{
		//使用反射做通用框架
		Properties prop = new Properties();
		//不使用ResourceBundle方法读取properties文件,工作目录为项目路径
		prop.load(new FileInputStream("./src/Reflection_frame/demo.properties"));
		//获取配置文件属性
		String className = prop.getProperty("className");
		String methodeName = prop.getProperty("methodeName");
		//打印测试
//		System.out.println(prop.getProperty("className"));
//		System.out.println(prop.getProperty("methodeName"));
		//使用反射
		//获取Class类对象
//		Class cls = Class.forName(className);
//		Method m1 = cls.getDeclaredMethod(methodeName);
		Method m1 = Class.forName(className).getDeclaredMethod(methodeName);
		m1.setAccessible(true);
		m1.invoke(Class.forName(className).newInstance());
	}
}
